package com.cql.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cql.common.config.FoodCircleConfig;
import com.cql.common.constant.FoodCircleConstant;
import com.cql.common.core.DO.SysRoleDO;
import com.cql.common.core.DTO.RoleDTO;
import com.cql.common.utils.StringUtils;
import com.cql.mapper.SysRoleMapper;
import com.cql.service.business.SysRoleService;
import com.cql.service.cache.CommonCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author cql
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleDO> implements SysRoleService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private CommonCacheService commonCacheService;

    @Autowired
    private FoodCircleConfig foodCircleConfig;

    @Override
    public Set<String> listRolePermissionByUserId(Long userId) {
        List<SysRoleDO> perms = roleMapper.listRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRoleDO perm : perms) {
            if (StringUtils.isNotNull(perm)) {
                /**
                 * 可能一个角色名称对应多个角色字符串
                 */
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<RoleDTO> getBaseRole() {
        /**
         * 先去redis中查询
         */
        List<RoleDTO> baseRole = commonCacheService.getBaseRole();
        if (StringUtils.isNotEmpty(baseRole)) {
            return baseRole;
        }

        /**
         * 获取食客，厨师，店家
         */
        List<RoleDTO> result = new ArrayList<>();

        SysRoleDO diner = roleMapper.selectById(FoodCircleConstant.BASE_ROLE_DINER);

        SysRoleDO chef = roleMapper.selectById(FoodCircleConstant.BASE_ROLE_CHEF);

        SysRoleDO store = roleMapper.selectById(FoodCircleConstant.BASE_ROLE_STORE);


        if (diner == null || chef == null || store == null) {
            logger.error("系统异常,diner | chef | store 角色被删除");
            throw new RuntimeException("系统繁忙,请稍后再试!");
        }


        RoleDTO dinerDto = new RoleDTO();
        BeanUtils.copyProperties(diner, dinerDto);
        dinerDto.setDefaultAvatar(foodCircleConfig.getDefaultDiner());

        RoleDTO chefDto = new RoleDTO();
        BeanUtils.copyProperties(chef, chefDto);
        chefDto.setDefaultAvatar(foodCircleConfig.getDefaultChef());

        RoleDTO storeDto = new RoleDTO();
        BeanUtils.copyProperties(store, storeDto);
        storeDto.setDefaultAvatar(foodCircleConfig.getDefaultStore());

        result.add(dinerDto);
        result.add(chefDto);
        result.add(storeDto);

        //存储进redis
        commonCacheService.insertBaseRole(result);

        return result;
    }
}